<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php include("db.php"); 	// Connect to the database server
dbConnect('kar_web1');   	// Use function in db.php to connect
include_once("http://02d2e0a.netsolhost.com/includes/timeout.php");?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Mass E-Mail Program</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<?php include("http://02d2e0a.netsolhost.com/includes/head.inc");  ?>
<script src="Scripts/AC_RunActiveContent.js" type="text/javascript"></script>
</head>
<div id="bodywidth">
<?php include("http://02d2e0a.netsolhost.com/includes/header.inc");  ?>
<p align="center">&nbsp;</p>
<p align="center">&nbsp;</p>
<p align="center">&nbsp;</p>
<?php include("http://02d2e0a.netsolhost.com/includes/leftmenu.php");
if (!isset($_SESSION['name']))
{ ?>
<div id="centercontent" style="width: 475px; height: auto">
<form method="GET" action="confirmlogin.php">
<table height="300px" width="100%" border=0 >
	<td align= center valign="top">
		<table height="300px" width="100%" border=0>
			<td align="center">
				<table width="100%" border=0>
				<td align="center">
				<font size=6>KAR Volunteer and Admin Login</font>
				</td>
			</td>
		</table>

		<br>

		<table width="400px" cellpadding="5%" border="0">
		<tr>
			<td align=right><font size=3><b>User Name:</b></font></td /align>
			<td><input type="text" name="user" size="20"></td>
		</tr>
		<tr>
			<td align=right><font size=3><b>Password:</b></td /align>
			<td><input type="password" name="password" size="20"></td>
		</tr>
		</table>

		<table width="100%" border="0">
		<tr>
			<td align=center><input type="submit" value="Login">
			
		</tr>
		</table>
	</td>
</table>
<?php 
}
else
{
?>
<iframe id="stateFrame"
  name="stateFrame"
  style="width:00px; height:0px; border: 0px"
  src="blank.html"></iframe>

<body>
<?php
$subject = mysql_real_escape_string($_POST['Subject']);
$message = mysql_real_escape_string($_POST['Message']);
$email = mysql_real_escape_string($_POST['FromAddress']);
$outbound = $_POST['email'];
$newout = array(" ");
$to = "";
	// Loop through the array passed in from massemail.php
	for($Counter = 0; $Counter < count($outbound) - 1; $Counter+=1)
	{
		if (isset($outbound['catfoster']))
		{
			$outCount = count($newout) - 1;
			$query = "SELECT email FROM volunteers WHERE cat_foster='y'";
			$result = @mysql_query($query);
			if (!$result) {
				echo("<p>Error performing query: " . mysql_error() . "</p>");
				exit();
			}
			while($em = mysql_fetch_row($result))
			{
				$newout[$outCount] = $em[0];
				$outCount+=2;
			}
		}
		elseif (isset($outbound['dogfoster']))
		{
			$outCount = count($newout) - 1;
			$query = "SELECT email FROM volunteers WHERE dog_foster='y'";
			$result = @mysql_query($query);
			if (!$result) {
				echo("<p>Error performing query: " . mysql_error() . "</p>");
				exit();
			}
			while($em = mysql_fetch_row($result))
			{
				$newout[$outCount] = $em[0];
				$outCount+=2;
			}
		}
		elseif (isset($outbound['catscreener']))
		{
			$outCount = count($newout) - 1;
			$query = "SELECT email FROM volunteers WHERE catscreener='y'";
			$result = @mysql_query($query);
			if (!$result) {
				echo("<p>Error performing query: " . mysql_error() . "</p>");
				exit();
			}
			while($em = mysql_fetch_row($result))
			{
				$newout[$outCount] = $em[0];
				$outCount+=2;
			}
		}
		elseif (isset($outbound['dogscreener']))
		{
			$outCount = count($newout) - 1;
			$query = "SELECT email FROM volunteers WHERE dog_screener='y'";
			$result = @mysql_query($query);
			if (!$result) {
				echo("<p>Error performing query: " . mysql_error() . "</p>");
				exit();
			}
			while($em = mysql_fetch_row($result))
			{
				$newout[$outCount] = $em[0];
				$outCount+=2;
			}
		}
		elseif (isset($outbound['webteam']))
		{
			$outCount = count($newout) - 1;
			$query = "SELECT email FROM volunteers WHERE 'webteam'";
			$result = @mysql_query($query);
			if (!$result) {
				echo("<p>Error performing query: " . mysql_error() . "</p>");
				exit();
			}
			while($em = mysql_fetch_row($result))
			{
				$newout[$outCount] = $em[0];
				$outCount+=2;
			}
		}
		elseif (isset($outbound['board']))
		{
			$outCount = count($newout) - 1;
			$query = "SELECT email FROM volunteers WHERE board='y'";
			$result = @mysql_query($query);
			if (!$result) {
				echo("<p>Error performing query: " . mysql_error() . "</p>");
				exit();
			}
			while($em = mysql_fetch_row($result))
			{
				$newout[$outCount] = $em[0];
				$outCount+=2;
			}
		}
		else
		{
			$outCount = count($newout) - 1;
			$query = "SELECT email FROM volunteers WHERE name='$outbound[$Counter]'";
			$result = @mysql_query($query);
			if (!$result) {
				echo("<p>Error performing query: " . mysql_error() . "</p>");
				exit();
			}
			while($em = mysql_fetch_row($result))
			{
				$newout[$outCount] = $em[0];
				$outCount+=2;
			}
		}
	}
	
	// Check for duplicate entries and purge them, so multiple copies aren't sent to one person.
	$newout = array_unique($newout);
	foreach ($newout as &$value)
	{
		$to .= $value . ", ";
	}
	$query = "SELECT email FROM volunteers WHERE name='$email'";
	$result = @mysql_query($query);
	if (!$result) {
		echo("<p>Error performing query: " . mysql_error() . "</p>");
		exit();
	}
	$from = mysql_fetch_row($result);
	$header = "From: " . $email ; 
	mail($to, $subject, $message, $header);
	
	
	echo "<div id=\"centercontent\" style=\"width: 475px; height: auto\">";
		echo "<table width=100% border=0>";
		echo "<tr height=60px><td valign=top>";
			echo "<table width=100% height=30px border=0>";
				echo "<tr height=60px><td align=center valign=top>";
					echo "<font size=6 face=\"Verdana\">E-Mail Sent!</font>";
					echo "<br><a href=\"admin.php\">Return to Admin Page</a>";
				echo "</td></tr>";
			echo "</table>";
		echo "</td></tr>";
		echo "</table>";
	echo "</div>";
	echo "</div>";
}
?>
</div>
</div>
</body>
</html>